package vn.edu.saigontech.ncms.web.materialFacilities;

import vn.edu.saigontech.ncms.model.system.Location;
import vn.edu.saigontech.ncms.model.system.Room;
import vn.edu.saigontech.ncms.service.LocationService;
import vn.edu.saigontech.ncms.service.RoomService;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class RoomAction extends BaseAction {
	
	private Long id;
	private String code;
	private String name;
	private int seatCount;
	private boolean active;
	private String comment;
	private Long locationId;
	
	public String info() {
		REQUEST().setAttribute("locations", LocationService.INSTANCE.findAllAreActive());
		REQUEST().setAttribute("room", RoomService.INSTANCE.findById(this.id));
		return SUCCESS;
	}
	
	public String showAdd() {
		REQUEST().setAttribute("locations", LocationService.INSTANCE.findAllAreActive());
		return SUCCESS;
	}
	
	public String add() {
		Room room = new Room();
		Location location = LocationService.INSTANCE.findById(this.locationId); 
		room.setLocation(location);
		room.setCode(code);
		room.setName(name);
		room.setSeatCount(seatCount);
		room.setActive(true);
		room.setComment(comment);
		room = RoomService.INSTANCE.persist(room);
		this.id = room.getId();
		
		this.addLoggingDetail(BaseAction.ADD, "logging.room.add", room.getCode());
		return SUCCESS;
	}
	
	public String showEdit() {
		return this.info();
	}
	
	public String edit() {
		Room room = RoomService.INSTANCE.findById(this.id);
		Location location = LocationService.INSTANCE.findById(this.locationId); 
		room.setLocation(location);
		room.setCode(code);
		room.setName(name);
		room.setSeatCount(seatCount);
		room.setActive(this.active);
		room.setComment(comment);
		room = RoomService.INSTANCE.persist(room);
		
		this.addLoggingDetail(BaseAction.EDIT, "logging.room.edit", room.getCode());
		return SUCCESS;
	}
	
	public String list() {
		REQUEST().setAttribute("rooms", RoomService.INSTANCE.findAll());
		return SUCCESS;
	}
	
	public String delete() {
		Room room = RoomService.INSTANCE.findById(this.id);
		String code = room.getCode();
		if(RoomService.INSTANCE.deleteById(this.id)) {
			this.setKeyMessage("confirm.deleteSuccessfully", "room");
		}
		else {
			this.setKeyMessage("confirm.deleteUnsuccessfully", "room");
		}
		this.addLoggingDetail(BaseAction.DELETE, "logging.room.delete", code);
		return SUCCESS;
	}
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getSeatCount() {
		return seatCount;
	}

	public void setSeatCount(int seatCount) {
		this.seatCount = seatCount;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public Long getLocationId() {
		return locationId;
	}

	public void setLocationId(Long locationId) {
		this.locationId = locationId;
	}
}
